<template>
  <div class="department-container">
    <div class="app-container">
      <!-- 具体页面结构 -->
      <page-tools>
        <!-- 插入到left插槽位置 -->
        <template #left>
          <span>总共{{ total }}条</span>
        </template>
        <template #right>
          <!-- 跳转可简写 ('/employees/import') 没有提示·-->
          <el-button type="warning" @click="$router.push('../employees/import')">导入excel</el-button>
          <el-button type="danger">导出excel</el-button>
          <el-button type="primary" @click="dialogVisible = true">新增员工</el-button>
        </template>
      </page-tools>
      <el-card style="margin-top:20px">
        <el-table :data="employees">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column>
            <template v-slot="{row}">
              <img :src="row.staffPhoto" class="avatar">
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式">
            <template v-slot="{row}">

              <!-- {{ row.formOfEmployment===1 ? '正式':'非正式' }} -->
              {{ formatEmployment(row.formOfEmployment) ||'未知' }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column sortable label="入职时间" prop="timeOfEntry" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button type="text">查看</el-button>
              <el-button type="text">分配角色</el-button>
              <el-button type="text" @click="hDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 弹窗 -->
      <el-dialog
        title="添加"
        :visible.sync="dialogVisible"
        width="50%"
        @closed="close"
      >
        <empDialog ref="addEmployee" @close="dialogVisible=false" @success="hSuccess" />
      </el-dialog>
      <!-- 分页 -->
      <el-pagination
        :current-page="q.page"
        :page-sizes="[5,10,15,20]"
        :page-size="q.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>
<script>
import { delEmployee, getEmployees } from '@/api/employees'
import employeesConst from '@/constant/employees'
import empDialog from '@/views/employees/empDialog.vue'
// const hireTypeMap = {}
// employeesConst.hireType.forEach(item => {
//   hireTypeMap[item.id] = item.value
// })

export default {
  components: {
    empDialog
  },
  data() {
    return {
      employees: [],
      total: 0,
      q: {
        page: 1,
        size: 5
      },
      dialogVisible: false
    }
  },
  created() {
    this.loadEmployees()
  },
  methods: {
    async loadEmployees() {
      const res = await getEmployees(this.q)
      // console.log(res)
      this.employees = res.data.rows
      this.total = res.data.total
    },
    formatEmployment(code) {
      // console.log(code)
      const result = employeesConst.hireType.find(item => item.id === code)
      // return result.value
      if (result) {
        return result.value
      } else {
        return '未知'
      }
      // return hireTypeMap[code]
    },
    async hDel(id) {
      try {
        await this.$confirm('真的删除吗', '提示', { type: 'warning' })
        const res = await delEmployee(id)
        // console.log(res)
        this.$message.success(res.message)
        if (this.employees.length === 1 && this.q.page > 1) {
          this.q.page--
        }
        this.loadEmployees()
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    handleCurrentChange(page) {
      this.q.page = page
      this.loadEmployees()
    },
    handleSizeChange(size) {
      this.q.size = size
      this.loadEmployees()
    },
    hSuccess() {
      if (this.total % this.q.size === 0) {
        this.total++
      }
      this.q.page = Math.ceil(this.total / this.q.size)
      this.loadEmployees()
      this.dialogVisible = false
    },
    // 弹窗关闭清空表单验证
    // 父组件中获取子组件函数表单
    close() {
      this.$refs.addEmployee.resetForm()
    }
  }
}
</script>
<style scoped>
.avatar{
width: 50px;
height: 50px;
border-radius: 50%;
}
</style>
